﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisNames : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IHisNameBLL BLL = new HisNameBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
		private Main main = new Main();

        private RadComboBox cmbOldPrefixes, cmbNewPrefixes;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbOldPrefixes = (RadComboBox)cbOldPrefixes.FindControl("cbPrefixes");
            cmbNewPrefixes = (RadComboBox)cbNewPrefixes.FindControl("cbPrefixes");
            lblEmpSeq.Text = Session["emp_seq"].ToString();

            if (DataItem != null)
            {
                cbOldPrefixes.ListPrefixesComboBox();
                cbNewPrefixes.ListPrefixesComboBox();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    cmbOldPrefixes.SelectedValue = DataBinder.Eval(DataItem, "OldPrefixes.Sequence").ToString();
                    cmbNewPrefixes.SelectedValue = DataBinder.Eval(DataItem, "NewPrefixes.Sequence").ToString();

                    txtFirstName.Text = DataBinder.Eval(DataItem, "OldName").ToString();
                    txtLastName.Text = DataBinder.Eval(DataItem, "OldLastName").ToString();
                    txtChangeDate.SetValue(DataBinder.Eval(DataItem, "ChangeDate"));
                }
                else 
                {
                    DataSet ds;
                    DataRowCollection drc;

                    ds = empBLL.LoadEmployees(Convert.ToInt32(lblEmpSeq.Text.Trim()), "", true, "");
                    drc = ds.Tables[0].Rows;
                    if (drc.Count > 0)
                    {
                        cmbOldPrefixes.SelectedValue = drc[0]["name_prefix_seq"].ToString();
                        txtFirstName.Text = drc[0]["first_name"].ToString();
                        txtLastName.Text = drc[0]["last_name"].ToString();
                    }
                
                }
            }
        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            BLL.Insert(lblEmpSeq.Text.Trim()
                , txtChangeDate.GetStringValue().Length > 0 ? DateTime.Parse(txtChangeDate.GetStringValue()) : (DateTime?)null
                , txtChangeRefNo.Text.Trim()
                , cmbOldPrefixes.SelectedValue
                , txtFirstName.Text.Trim()
                , txtLastName.Text.Trim()
                , cmbNewPrefixes.SelectedValue
                , txtNewName.Text.Trim()
                , txtNewLastName.Text.Trim()
                , txtRemarks.Text
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            BLL.Update(Int32.Parse(lblSequence.Text.Trim())
                , lblEmpSeq.Text.Trim()
                , txtChangeDate.GetStringValue().Length > 0 ? DateTime.Parse(txtChangeDate.GetStringValue()) : (DateTime?)null
                , txtChangeRefNo.Text.Trim()
                , cmbOldPrefixes.SelectedValue
                , txtFirstName.Text.Trim()
                , txtLastName.Text.Trim()
                , cmbNewPrefixes.SelectedValue
                , txtNewName.Text.Trim()
                , txtNewLastName.Text.Trim()
                , txtRemarks.Text
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

    }
}